package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.AppUser;
import com.leoman.service.AppUserService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 普通用户管理
 * Created by 涂奕恒 on 2016/10/21.
 */
@Controller
@RequestMapping(value = "backend/appUser")
public class AppUserController extends CommonController {

    @Autowired
    private AppUserService appUserService;

    @RequestMapping("index")
    public String index() {
        return "backend/APP用户列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String mobile,
                     String nickName,
                     Integer sex,
                     Integer status,
                     Integer provinceId,
                     Integer cityId,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<AppUser> page = appUserService.pageByParams(mobile, nickName, sex, status, provinceId, cityId, pageNum, length);

        // 如果所属地id不为空，则查询该用户的所属地详细信息
        for (AppUser appUser : page.getContent()) {
            if (null != appUser.getRegion()) {
                appUser.setAliPayAccount(appUser.getRegion().getCity().getProvince().getName() + appUser.getRegion().getCity().getName() + appUser.getRegion().getName());
            } else {
                appUser.setAliPayAccount("");
            }
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer appUserId) {
        if (null != appUserId) {
            AppUser appUser = appUserService.getById(appUserId);
            model.addAttribute("appUser", appUser);
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(appUser.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
            try {
                model.addAttribute("userResume", appUserService.iFindOneByResumeInfo(appUserId));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "backend/APP用户详情";
    }

    /**
     * 启用 or 禁用APP用户
     *
     * @param appUserId 用户ID
     * @param status    状态
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Integer changeStatus(Integer appUserId, Integer status) {
        try {
            Integer[] appUserIds = new Integer[1];
            appUserIds[0] = appUserId;
            return appUserService.changeAppUserStatus(appUserIds, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量禁用会员信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Integer batchUpdate(String ids, Integer status) {
        try {
            Integer[] arrayId = JsonUtil.json2Obj(ids, Integer[].class);
            return appUserService.changeAppUserStatus(arrayId, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
